package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.mapper.DtsAdminMapper;
import com.qf.mapper.DtsRoleMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsRole;
import com.qf.service.DtsAdminService;
import com.qf.vo.CatVo;
import jdk.nashorn.internal.ir.CallNode;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author 邢式瑞
 * @version 1.0
 * @project E-commercebackGroundManagementSystem
 * @date 2022/9/20 19:26
 */
@Service
public class DtsAdminServiceImpl extends ServiceImpl<BaseMapper<DtsAdmin>,DtsAdmin> implements DtsAdminService {

@Autowired
        private DtsAdminMapper dtsAdminMapper;

    @Override
    public DtsAdmin findDtsAdminByUsername(String username) {
        return getBaseMapper().selectOne(new QueryWrapper<DtsAdmin>().eq("username",username));
    }

    @Override
    public Page<DtsAdmin> selectAdminPage(Integer page, Integer limit, String sort, String order, String username) {
        Page<DtsAdmin> dtsAdminPage = new Page<>(page, limit);
        QueryWrapper<DtsAdmin> dtsAdminQueryWrapper = new QueryWrapper<>();
        dtsAdminQueryWrapper.orderBy(!StringUtils.isEmpty(sort),"asc".equalsIgnoreCase(order),sort).like(!StringUtils.isEmpty(username),"username",username).eq("deleted",false);

        return dtsAdminMapper.selectPage(dtsAdminPage,dtsAdminQueryWrapper);
    }

    @Override
    public void updateAdmin(DtsAdmin dtsAdmin) {

        dtsAdminMapper.updateById(dtsAdmin);
    }

    @Override
    public void insertAdmin(DtsAdmin dtsAdmin) {
        dtsAdminMapper.insert(dtsAdmin);
    }

    @Override
    public void deleteAdmin(DtsAdmin dtsAdmin) {
        dtsAdminMapper.updateById(dtsAdmin);
    }

    @Override
    public Set<CatVo> selectAdminList() {
        List<DtsAdmin> dtsAdmins = dtsAdminMapper.selectList(null);

       return  dtsAdmins.stream().map(dtsAdmin -> {
                    CatVo catVo = new CatVo();
                    catVo.setValue(dtsAdmin.getId());
                    catVo.setLabel(dtsAdmin.getUsername());
                    return catVo;
                }
        ).collect(Collectors.toSet());
    }

    @Override
    public DtsAdmin selectAdminByPwd(String encode) {
        return dtsAdminMapper.selectOne(new QueryWrapper<DtsAdmin>().eq("password",encode).eq("deleted",false));
    }


}

